package com.cb2.algorithm.leetcode;

import java.util.ArrayDeque;
import java.util.LinkedList;
import java.util.Queue;

/**
 * <a href="https://leetcode.cn/problems/same-tree/">相同的树(Same Tree)</a>
 * <p>给你两棵二叉树的根节点 p 和 q ，编写一个函数来检验这两棵树是否相同。</p>
 * <p>如果两个树在结构上相同，并且节点具有相同的值，则认为它们是相同的。</p>
 *
 * <p>
 * <b>示例：</b>
 * <pre>
 * 示例 1：
 *      输入：p = [1,2,3], q = [1,2,3]
 *      输出：true
 *
 * 示例 2：
 *      输入：p = [1,2], q = [1,null,2]
 *      输出：false
 *
 * 示例 3：
 *      输入：p = [1,2,1], q = [1,1,2]
 *      输出：false
 * </pre>
 * </p>
 *
 * <p>
 * <b>提示：</b>
 *     <ul>
 *         <li>两棵树上的节点数目都在范围 [0, 100] 内</li><li>-10^4 <= Node.val <= 10^4</li>
 *     </ul>
 * </p>
 *
 * @author c2b
 * @since 2025/2/18 16:23
 */
public class LC0100SameTree_S {
    static class Solution {
        public boolean isSameTree(TreeNode p, TreeNode q) {
            //return isSameTreeByIteration(p, q);
            return isSameTreeByRecursion(p, q);
        }

        private boolean isSameTreeByIteration(TreeNode p, TreeNode q) {
            Queue<TreeNode> helper1 = new LinkedList<>();
            helper1.offer(p);
            Queue<TreeNode> helper2 = new LinkedList<>();
            helper2.offer(q);
            while (!helper1.isEmpty() && !helper2.isEmpty()) {
                TreeNode pNode = helper1.poll();
                TreeNode qNode = helper2.poll();
                if (pNode != null && qNode != null) {
                    if (pNode.val != qNode.val) {
                        return false;
                    }
                    helper1.offer(pNode.left);
                    helper1.offer(pNode.right);
                    helper2.offer(qNode.left);
                    helper2.offer(qNode.right);
                } else if (pNode != null || qNode != null) {
                    return false;
                }
            }
            return true;
        }

        private boolean isSameTreeByRecursion(TreeNode p, TreeNode q) {
            if (p == null && q == null) {
                return true;
            }
            if (p == null || q == null || p.val != q.val) {
                return false;
            }
            return isSameTreeByRecursion(p.left, q.left) && isSameTreeByRecursion(p.right, q.right);
        }
    }

    public static void main(String[] args) {
        TreeNode p1 = new TreeNode(1);
        p1.left = new TreeNode(2);
        p1.right = new TreeNode(3);
        TreeNode q1 = new TreeNode(1);
        q1.left = new TreeNode(2);
        q1.right = new TreeNode(3);

        TreeNode p2 = new TreeNode(1);
        p2.left = new TreeNode(2);
        TreeNode q2 = new TreeNode(1);
        q2.right = new TreeNode(2);

        TreeNode p3 = new TreeNode(1);
        p3.left = new TreeNode(2);
        p3.right = new TreeNode(1);
        TreeNode q3 = new TreeNode(1);
        q3.left = new TreeNode(1);
        q3.right = new TreeNode(2);

        Solution solution = new Solution();
        System.out.println(solution.isSameTree(p1, q1));
        System.out.println(solution.isSameTree(p2, q2));
        System.out.println(solution.isSameTree(p3, q3));
    }
}
